/*
 *  NFCSigning - Open source library for signing/validation of NDEF messages
 *  Copyright (C) 2009-2010 The NFCSigning Team
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */
package sampleappa;

import com.ericsson.nfc.signing.certificate.CertificateChain;
import com.ericsson.nfc.signing.certificate.X509Certificate;
import com.ericsson.nfc.signing.certificate.X509CertificateChain;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import org.bouncycastle.asn1.x509.X509Name;

/**
 * @author emarkki
 */
public class CertificateVisualDesign implements CommandListener {

    private MIDlet midlet;
    private CertificateChain chain;
    private Displayable previousDisplay;

//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
private java.util.Hashtable __previousDisplayables = new java.util.Hashtable ();
private Form formCertificate;
private StringItem stringItemSubject;
private StringItem stringItemIssuer;
private StringItem stringItemNotBefore;
private StringItem stringItemNotAfter;
private Form formCertificateDetails;
private ChoiceGroup choiceGroupChain;
private StringItem stringItemCertVersion;
private StringItem stringItemCertSerial;
private StringItem stringItemCertNotBefore;
private StringItem stringItemCertNotAfter;
private StringItem stringItemCertAlgorithm;
private StringItem stringItemCertIssuer;
private StringItem stringItemCertCertSignature;
private StringItem stringItemPublicKey;
private StringItem stringItemCertSubject;
private StringItem stringItemCertCertAlg;
private StringItem stringItemPublicKeyAlg;
private Form formData;
private StringItem stringItemData;
private Command screenCommand;
private Command itemCommandDetails;
private Command backCommand;
private Command backCommand1;
private Command itemCommand;
private Command backCommand2;
//</editor-fold>//GEN-END:|fields|0|
    
//</editor-fold>

    /**
     * The VisualDesign2 constructor.
     * @param midlet the midlet used for getting
     */
    public CertificateVisualDesign (MIDlet midlet) {
        this.midlet = midlet;
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
/**
 * Switches a display to previous displayable of the current displayable.
 * The <code>display</code> instance is obtain from the <code>getDisplay</code> method.
 */
private void switchToPreviousDisplayable () {
Displayable __currentDisplayable = getDisplay ().getCurrent ();
if (__currentDisplayable != null) {
Displayable __nextDisplayable = (Displayable) __previousDisplayables.get (__currentDisplayable);
if (__nextDisplayable != null) {
switchDisplayable (null, __nextDisplayable);
}
}
}
//</editor-fold>//GEN-END:|methods|0|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
/**
 * Initilizes the application.
 * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
 */
private void initialize () {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
}//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|2-switchDisplayable|0|2-preSwitch
/**
 * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
 * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
 * @param nextDisplayable the Displayable to be set
 */
public void switchDisplayable (Alert alert, Displayable nextDisplayable) {//GEN-END:|2-switchDisplayable|0|2-preSwitch
        // write pre-switch user code here
Display display = getDisplay ();//GEN-BEGIN:|2-switchDisplayable|1|2-postSwitch
Displayable __currentDisplayable = display.getCurrent ();
if (__currentDisplayable != null  &&  nextDisplayable != null) {
__previousDisplayables.put (nextDisplayable, __currentDisplayable);
}
if (alert == null) {
display.setCurrent (nextDisplayable);
} else {
display.setCurrent (alert, nextDisplayable);
}//GEN-END:|2-switchDisplayable|1|2-postSwitch
        // write post-switch user code here
}//GEN-BEGIN:|2-switchDisplayable|2|
//</editor-fold>//GEN-END:|2-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|4-commandAction|0|4-preCommandAction
/**
 * Called by a system to indicated that a command has been invoked on a particular displayable.
 * @param command the Command that was invoked
 * @param displayable the Displayable where the command was invoked
 */
public void commandAction (Command command, Displayable displayable) {//GEN-END:|4-commandAction|0|4-preCommandAction
 // write pre-action user code here
if (displayable == formCertificate) {//GEN-BEGIN:|4-commandAction|1|20-preAction
if (command == backCommand) {//GEN-END:|4-commandAction|1|20-preAction
 // write pre-action user code here
backToMIDlet();//GEN-LINE:|4-commandAction|2|20-postAction
 // write post-action user code here
} else if (command == itemCommandDetails) {//GEN-LINE:|4-commandAction|3|18-preAction
 // write pre-action user code here
switchDisplayable (null, getFormCertificateDetails ());//GEN-LINE:|4-commandAction|4|18-postAction
 // write post-action user code here
}//GEN-BEGIN:|4-commandAction|5|26-preAction
} else if (displayable == formCertificateDetails) {
if (command == backCommand1) {//GEN-END:|4-commandAction|5|26-preAction
 // write pre-action user code here
switchDisplayable (null, getFormCertificate ());//GEN-LINE:|4-commandAction|6|26-postAction
 // write post-action user code here
}//GEN-BEGIN:|4-commandAction|7|50-preAction
} else if (displayable == formData) {
if (command == backCommand2) {//GEN-END:|4-commandAction|7|50-preAction
 // write pre-action user code here
switchToPreviousDisplayable ();//GEN-LINE:|4-commandAction|8|50-postAction
 // write post-action user code here
}//GEN-BEGIN:|4-commandAction|9|4-postCommandAction
}//GEN-END:|4-commandAction|9|4-postCommandAction
 // write post-action user code here
}//GEN-BEGIN:|4-commandAction|10|
//</editor-fold>//GEN-END:|4-commandAction|10|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: formCertificate ">//GEN-BEGIN:|10-getter|0|10-preInit
/**
 * Returns an initiliazed instance of formCertificate component.
 * @return the initialized component instance
 */
public Form getFormCertificate () {
if (formCertificate == null) {//GEN-END:|10-getter|0|10-preInit
 // write pre-init user code here
formCertificate = new Form ("Certificate", new Item[] { getStringItemSubject (), getStringItemIssuer (), getStringItemNotBefore (), getStringItemNotAfter () });//GEN-BEGIN:|10-getter|1|10-postInit
formCertificate.addCommand (getItemCommandDetails ());
formCertificate.addCommand (getBackCommand ());
formCertificate.setCommandListener (this);//GEN-END:|10-getter|1|10-postInit
 // write post-init user code here
}//GEN-BEGIN:|10-getter|2|
return formCertificate;
}
//</editor-fold>//GEN-END:|10-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemSubject ">//GEN-BEGIN:|11-getter|0|11-preInit
/**
 * Returns an initiliazed instance of stringItemSubject component.
 * @return the initialized component instance
 */
public StringItem getStringItemSubject () {
if (stringItemSubject == null) {//GEN-END:|11-getter|0|11-preInit
 // write pre-init user code here
stringItemSubject = new StringItem ("Issued to:", null);//GEN-LINE:|11-getter|1|11-postInit
 // write post-init user code here
}//GEN-BEGIN:|11-getter|2|
return stringItemSubject;
}
//</editor-fold>//GEN-END:|11-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemIssuer ">//GEN-BEGIN:|12-getter|0|12-preInit
/**
 * Returns an initiliazed instance of stringItemIssuer component.
 * @return the initialized component instance
 */
public StringItem getStringItemIssuer () {
if (stringItemIssuer == null) {//GEN-END:|12-getter|0|12-preInit
 // write pre-init user code here
stringItemIssuer = new StringItem ("Issued by:", null);//GEN-LINE:|12-getter|1|12-postInit
 // write post-init user code here
}//GEN-BEGIN:|12-getter|2|
return stringItemIssuer;
}
//</editor-fold>//GEN-END:|12-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemNotBefore ">//GEN-BEGIN:|13-getter|0|13-preInit
/**
 * Returns an initiliazed instance of stringItemNotBefore component.
 * @return the initialized component instance
 */
public StringItem getStringItemNotBefore () {
if (stringItemNotBefore == null) {//GEN-END:|13-getter|0|13-preInit
 // write pre-init user code here
stringItemNotBefore = new StringItem ("Valid from", null, Item.PLAIN);//GEN-LINE:|13-getter|1|13-postInit
 // write post-init user code here
}//GEN-BEGIN:|13-getter|2|
return stringItemNotBefore;
}
//</editor-fold>//GEN-END:|13-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemNotAfter ">//GEN-BEGIN:|14-getter|0|14-preInit
/**
 * Returns an initiliazed instance of stringItemNotAfter component.
 * @return the initialized component instance
 */
public StringItem getStringItemNotAfter () {
if (stringItemNotAfter == null) {//GEN-END:|14-getter|0|14-preInit
 // write pre-init user code here
stringItemNotAfter = new StringItem ("to", null);//GEN-LINE:|14-getter|1|14-postInit
 // write post-init user code here
}//GEN-BEGIN:|14-getter|2|
return stringItemNotAfter;
}
//</editor-fold>//GEN-END:|14-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: screenCommand ">//GEN-BEGIN:|15-getter|0|15-preInit
/**
 * Returns an initiliazed instance of screenCommand component.
 * @return the initialized component instance
 */
public Command getScreenCommand () {
if (screenCommand == null) {//GEN-END:|15-getter|0|15-preInit
 // write pre-init user code here
screenCommand = new Command ("Screen", Command.SCREEN, 0);//GEN-LINE:|15-getter|1|15-postInit
 // write post-init user code here
}//GEN-BEGIN:|15-getter|2|
return screenCommand;
}
//</editor-fold>//GEN-END:|15-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommandDetails ">//GEN-BEGIN:|17-getter|0|17-preInit
/**
 * Returns an initiliazed instance of itemCommandDetails component.
 * @return the initialized component instance
 */
public Command getItemCommandDetails () {
if (itemCommandDetails == null) {//GEN-END:|17-getter|0|17-preInit
 // write pre-init user code here
itemCommandDetails = new Command ("Details", Command.ITEM, 0);//GEN-LINE:|17-getter|1|17-postInit
 // write post-init user code here
}//GEN-BEGIN:|17-getter|2|
return itemCommandDetails;
}
//</editor-fold>//GEN-END:|17-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|19-getter|0|19-preInit
/**
 * Returns an initiliazed instance of backCommand component.
 * @return the initialized component instance
 */
public Command getBackCommand () {
if (backCommand == null) {//GEN-END:|19-getter|0|19-preInit
 // write pre-init user code here
backCommand = new Command ("Back", Command.BACK, 0);//GEN-LINE:|19-getter|1|19-postInit
 // write post-init user code here
}//GEN-BEGIN:|19-getter|2|
return backCommand;
}
//</editor-fold>//GEN-END:|19-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: formCertificateDetails ">//GEN-BEGIN:|21-getter|0|21-preInit
/**
 * Returns an initiliazed instance of formCertificateDetails component.
 * @return the initialized component instance
 */
public Form getFormCertificateDetails () {
if (formCertificateDetails == null) {//GEN-END:|21-getter|0|21-preInit
 // write pre-init user code here
formCertificateDetails = new Form ("Certificate details", new Item[] { getChoiceGroupChain (), getStringItemCertVersion (), getStringItemCertSerial (), getStringItemCertAlgorithm (), getStringItemCertIssuer (), getStringItemCertNotBefore (), getStringItemCertNotAfter (), getStringItemCertSubject (), getStringItemPublicKeyAlg (), getStringItemPublicKey (), getStringItemCertCertAlg (), getStringItemCertCertSignature () });//GEN-BEGIN:|21-getter|1|21-postInit
formCertificateDetails.addCommand (getBackCommand1 ());
formCertificateDetails.setCommandListener (this);//GEN-END:|21-getter|1|21-postInit
 // write post-init user code here
formCertificateDetails.setItemStateListener(new ItemStateListener() {

                public void itemStateChanged(Item item) {
                    formCertificateDetailsItemStateChangedPerformed(item);
                }
            });
}//GEN-BEGIN:|21-getter|2|
return formCertificateDetails;
}
//</editor-fold>//GEN-END:|21-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroupChain ">//GEN-BEGIN:|22-getter|0|22-preInit
/**
 * Returns an initiliazed instance of choiceGroupChain component.
 * @return the initialized component instance
 */
public ChoiceGroup getChoiceGroupChain () {
if (choiceGroupChain == null) {//GEN-END:|22-getter|0|22-preInit
 // write pre-init user code here
choiceGroupChain = new ChoiceGroup ("Chain", Choice.EXCLUSIVE);//GEN-BEGIN:|22-getter|1|22-postInit
choiceGroupChain.setFitPolicy (Choice.TEXT_WRAP_DEFAULT);//GEN-END:|22-getter|1|22-postInit
 // write post-init user code here
}//GEN-BEGIN:|22-getter|2|
return choiceGroupChain;
}
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|25-getter|0|25-preInit
/**
 * Returns an initiliazed instance of backCommand1 component.
 * @return the initialized component instance
 */
public Command getBackCommand1 () {
if (backCommand1 == null) {//GEN-END:|25-getter|0|25-preInit
 // write pre-init user code here
backCommand1 = new Command ("Back", Command.BACK, 0);//GEN-LINE:|25-getter|1|25-postInit
 // write post-init user code here
}//GEN-BEGIN:|25-getter|2|
return backCommand1;
}
//</editor-fold>//GEN-END:|25-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertVersion ">//GEN-BEGIN:|34-getter|0|34-preInit
/**
 * Returns an initiliazed instance of stringItemCertVersion component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertVersion () {
if (stringItemCertVersion == null) {//GEN-END:|34-getter|0|34-preInit
 // write pre-init user code here
stringItemCertVersion = new StringItem ("Version:", null, Item.PLAIN);//GEN-BEGIN:|34-getter|1|34-postInit
stringItemCertVersion.setLayout (ImageItem.LAYOUT_DEFAULT);//GEN-END:|34-getter|1|34-postInit
 // write post-init user code here
}//GEN-BEGIN:|34-getter|2|
return stringItemCertVersion;
}
//</editor-fold>//GEN-END:|34-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertSerial ">//GEN-BEGIN:|35-getter|0|35-preInit
/**
 * Returns an initiliazed instance of stringItemCertSerial component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertSerial () {
if (stringItemCertSerial == null) {//GEN-END:|35-getter|0|35-preInit
 // write pre-init user code here
stringItemCertSerial = new StringItem ("Serial number:", null);//GEN-LINE:|35-getter|1|35-postInit
 // write post-init user code here
}//GEN-BEGIN:|35-getter|2|
return stringItemCertSerial;
}
//</editor-fold>//GEN-END:|35-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertAlgorithm ">//GEN-BEGIN:|36-getter|0|36-preInit
/**
 * Returns an initiliazed instance of stringItemCertAlgorithm component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertAlgorithm () {
if (stringItemCertAlgorithm == null) {//GEN-END:|36-getter|0|36-preInit
 // write pre-init user code here
stringItemCertAlgorithm = new StringItem ("Algorithm:", null);//GEN-LINE:|36-getter|1|36-postInit
 // write post-init user code here
}//GEN-BEGIN:|36-getter|2|
return stringItemCertAlgorithm;
}
//</editor-fold>//GEN-END:|36-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertIssuer ">//GEN-BEGIN:|37-getter|0|37-preInit
/**
 * Returns an initiliazed instance of stringItemCertIssuer component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertIssuer () {
if (stringItemCertIssuer == null) {//GEN-END:|37-getter|0|37-preInit
 // write pre-init user code here
stringItemCertIssuer = new StringItem ("Issuer:", null);//GEN-LINE:|37-getter|1|37-postInit
 // write post-init user code here
}//GEN-BEGIN:|37-getter|2|
return stringItemCertIssuer;
}
//</editor-fold>//GEN-END:|37-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertNotBefore ">//GEN-BEGIN:|38-getter|0|38-preInit
/**
 * Returns an initiliazed instance of stringItemCertNotBefore component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertNotBefore () {
if (stringItemCertNotBefore == null) {//GEN-END:|38-getter|0|38-preInit
 // write pre-init user code here
stringItemCertNotBefore = new StringItem ("Valid from:", null);//GEN-LINE:|38-getter|1|38-postInit
 // write post-init user code here
}//GEN-BEGIN:|38-getter|2|
return stringItemCertNotBefore;
}
//</editor-fold>//GEN-END:|38-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertNotAfter ">//GEN-BEGIN:|39-getter|0|39-preInit
/**
 * Returns an initiliazed instance of stringItemCertNotAfter component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertNotAfter () {
if (stringItemCertNotAfter == null) {//GEN-END:|39-getter|0|39-preInit
 // write pre-init user code here
stringItemCertNotAfter = new StringItem ("Valid to:", null);//GEN-LINE:|39-getter|1|39-postInit
 // write post-init user code here
}//GEN-BEGIN:|39-getter|2|
return stringItemCertNotAfter;
}
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertSubject ">//GEN-BEGIN:|40-getter|0|40-preInit
/**
 * Returns an initiliazed instance of stringItemCertSubject component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertSubject () {
if (stringItemCertSubject == null) {//GEN-END:|40-getter|0|40-preInit
 // write pre-init user code here
stringItemCertSubject = new StringItem ("Subject:", null);//GEN-LINE:|40-getter|1|40-postInit
 // write post-init user code here
}//GEN-BEGIN:|40-getter|2|
return stringItemCertSubject;
}
//</editor-fold>//GEN-END:|40-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertCertAlg ">//GEN-BEGIN:|41-getter|0|41-preInit
/**
 * Returns an initiliazed instance of stringItemCertCertAlg component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertCertAlg () {
if (stringItemCertCertAlg == null) {//GEN-END:|41-getter|0|41-preInit
 // write pre-init user code here
stringItemCertCertAlg = new StringItem ("Signature algorithm:", null);//GEN-LINE:|41-getter|1|41-postInit
 // write post-init user code here
}//GEN-BEGIN:|41-getter|2|
return stringItemCertCertAlg;
}
//</editor-fold>//GEN-END:|41-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemCertCertSignature ">//GEN-BEGIN:|42-getter|0|42-preInit
/**
 * Returns an initiliazed instance of stringItemCertCertSignature component.
 * @return the initialized component instance
 */
public StringItem getStringItemCertCertSignature () {
if (stringItemCertCertSignature == null) {//GEN-END:|42-getter|0|42-preInit
 // write pre-init user code here
stringItemCertCertSignature = new StringItem ("Signature:", "Show signature", Item.BUTTON);//GEN-LINE:|42-getter|1|42-postInit
 // write post-init user code here
}//GEN-BEGIN:|42-getter|2|
return stringItemCertCertSignature;
}
//</editor-fold>//GEN-END:|42-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemPublicKey ">//GEN-BEGIN:|43-getter|0|43-preInit
/**
 * Returns an initiliazed instance of stringItemPublicKey component.
 * @return the initialized component instance
 */
public StringItem getStringItemPublicKey () {
if (stringItemPublicKey == null) {//GEN-END:|43-getter|0|43-preInit
 // write pre-init user code here
stringItemPublicKey = new StringItem ("Public key:", "Show key", Item.BUTTON);//GEN-LINE:|43-getter|1|43-postInit
 // write post-init user code here
}//GEN-BEGIN:|43-getter|2|
return stringItemPublicKey;
}
//</editor-fold>//GEN-END:|43-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemPublicKeyAlg ">//GEN-BEGIN:|44-getter|0|44-preInit
/**
 * Returns an initiliazed instance of stringItemPublicKeyAlg component.
 * @return the initialized component instance
 */
public StringItem getStringItemPublicKeyAlg () {
if (stringItemPublicKeyAlg == null) {//GEN-END:|44-getter|0|44-preInit
 // write pre-init user code here
stringItemPublicKeyAlg = new StringItem ("Public key algorithm:", null);//GEN-LINE:|44-getter|1|44-postInit
 // write post-init user code here
}//GEN-BEGIN:|44-getter|2|
return stringItemPublicKeyAlg;
}
//</editor-fold>//GEN-END:|44-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: formData ">//GEN-BEGIN:|45-getter|0|45-preInit
/**
 * Returns an initiliazed instance of formData component.
 * @return the initialized component instance
 */
public Form getFormData () {
if (formData == null) {//GEN-END:|45-getter|0|45-preInit
 // write pre-init user code here
formData = new Form ("Data", new Item[] { getStringItemData () });//GEN-BEGIN:|45-getter|1|45-postInit
formData.addCommand (getBackCommand2 ());
formData.setCommandListener (this);//GEN-END:|45-getter|1|45-postInit
 // write post-init user code here
}//GEN-BEGIN:|45-getter|2|
return formData;
}
//</editor-fold>//GEN-END:|45-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: stringItemData ">//GEN-BEGIN:|48-getter|0|48-preInit
/**
 * Returns an initiliazed instance of stringItemData component.
 * @return the initialized component instance
 */
public StringItem getStringItemData () {
if (stringItemData == null) {//GEN-END:|48-getter|0|48-preInit
 // write pre-init user code here
stringItemData = new StringItem ("Data:", null);//GEN-LINE:|48-getter|1|48-postInit
 // write post-init user code here
}//GEN-BEGIN:|48-getter|2|
return stringItemData;
}
//</editor-fold>//GEN-END:|48-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|46-getter|0|46-preInit
/**
 * Returns an initiliazed instance of itemCommand component.
 * @return the initialized component instance
 */
public Command getItemCommand () {
if (itemCommand == null) {//GEN-END:|46-getter|0|46-preInit
 // write pre-init user code here
itemCommand = new Command ("Item", Command.ITEM, 0);//GEN-LINE:|46-getter|1|46-postInit
 // write post-init user code here
}//GEN-BEGIN:|46-getter|2|
return itemCommand;
}
//</editor-fold>//GEN-END:|46-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|49-getter|0|49-preInit
/**
 * Returns an initiliazed instance of backCommand2 component.
 * @return the initialized component instance
 */
public Command getBackCommand2 () {
if (backCommand2 == null) {//GEN-END:|49-getter|0|49-preInit
 // write pre-init user code here
backCommand2 = new Command ("Back", Command.BACK, 0);//GEN-LINE:|49-getter|1|49-postInit
 // write post-init user code here
}//GEN-BEGIN:|49-getter|2|
return backCommand2;
}
//</editor-fold>//GEN-END:|49-getter|2|


public void showCertificateChain(X509CertificateChain chain, Displayable previousDisplay) {
    this.chain = chain;
    this.previousDisplay = previousDisplay;
    switchDisplayable(null, getFormCertificate());
    
    X509Certificate cert = (X509Certificate) chain.getUserCertificate();
    
    stringItemSubject.setText(Utils.getCommonName(cert.getStructure().getSubject()));
    stringItemIssuer.setText(Utils.getCommonName(cert.getStructure().getIssuer()));
    stringItemNotBefore.setText(cert.getStructure().getStartDate().getTime());
    stringItemNotAfter.setText(cert.getStructure().getEndDate().getTime());
    
    getChoiceGroupChain().deleteAll();
    X509Certificate[] certs = (X509Certificate[]) chain.getCertificates();
    for(int i = 0; i < certs.length; i++) {
        getChoiceGroupChain().append(certs[i].getStructure().getSubject().toString(true, X509Name.DefaultSymbols), null);
    }
    
    updateCertDetails(cert);
}



    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(midlet);
    }

    /**
     * Exits MIDlet.
     * Note you have to implement proper MIDlet destroying.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        // midlet.destroyApp(true);
        midlet.notifyDestroyed();
    }
    
    private void backToMIDlet() {
        switchDisplayable(null, previousDisplay);
    }

    private void formCertificateDetailsItemStateChangedPerformed(Item item) {
        
        if(item == choiceGroupChain) {
            int sel = choiceGroupChain.getSelectedIndex();
            updateCertDetails((X509Certificate) chain.getCertificates()[sel]);
        }
    }

    private void updateCertDetails(final X509Certificate cert) {
        getStringItemCertVersion().setText(""+cert.getStructure().getVersion());
        getStringItemCertSerial().setText(cert.getStructure().getSerialNumber().toString());
        getStringItemCertAlgorithm().setText(X509Certificate.getAlgorithmName(cert.getStructure().getSubjectPublicKeyInfo().getAlgorithmId()));
        getStringItemCertIssuer().setText(formatName(cert.getStructure().getIssuer()));
        getStringItemCertNotBefore().setText(cert.getStructure().getStartDate().getTime()); 
        getStringItemCertNotAfter().setText(cert.getStructure().getEndDate().getTime());
        getStringItemCertSubject().setText(formatName(cert.getStructure().getSubject()));
        
        getStringItemPublicKeyAlg().setText(X509Certificate.getAlgorithmName(cert.getStructure().getSubjectPublicKeyInfo().getAlgorithmId()));
        getStringItemPublicKey().setDefaultCommand(new Command("Show key", Command.ITEM, 1));
        getStringItemPublicKey().setItemCommandListener(new ItemCommandListener() {
            public void commandAction(Command arg0, Item arg1) {
                String key = Utils.toHexString(cert.getStructure().getSubjectPublicKeyInfo().getPublicKeyData().getBytes(), true);
                getStringItemData().setLabel("Public key:");
                getStringItemData().setText(key);
                switchDisplayable(null, getFormData());
            }
        });
        
        getStringItemCertCertAlg().setText(X509Certificate.getAlgorithmName(cert.getStructure().getSignatureAlgorithm()));
        getStringItemCertCertSignature().setDefaultCommand(new Command("Show signature", Command.ITEM, 1));
        getStringItemCertCertSignature().setItemCommandListener(new ItemCommandListener() {
            public void commandAction(Command arg0, Item arg1) {
                String key = Utils.toHexString(cert.getStructure().getSignature().getBytes(), true);
                getStringItemData().setLabel("Signature:");
                getStringItemData().setText(key);
                switchDisplayable(null, getFormData());
            }
        });
        
    }
    
    private static String formatName(X509Name name) {
        char[] formatted = name.toString().toCharArray();
        for(int i = 0; i < formatted.length; i++) {
            if(formatted[i] == ',') {
                formatted[i] = '\n';
            }
        }
        return new String(formatted);
    }
    
}
